# -*- coding: utf-8 -*-
"""
Created on 2024-04-25 12:25:26
---------
@summary:
---------
@author: Alex
"""
from venv import logger
from items.amazon_item_category import AmazonCategoryItem, AmazonCategoryAsinsItem
import feapder
from spiders.amazon.amazon_spider_base import BaseSpider


class AmazonCategorySpider(BaseSpider):
    host = "https://www.amazon.com"  # 补充相对url
    seed_table = "amazon_task_seeds"

    def start_requests_old(self):
        task_urls = ["https://www.amazon.com/gp/most-gifted/ref=zg_bs_tab_mg"]

        tasks = {"Best seller": "https://www.amazon.com/gp/bestsellers/",
                 #  "New releases":"https://www.amazon.com/gp/new-releases/",
                 #  "Movers & Shakers":"https://www.amazon.com/gp/movers-and-shakers/",
                 #  "Most Wished For":"https://www.amazon.com/gp/most-wished-for/ ",
                 #  "Gift Ideas":"https://www.amazon.com/gp/most-gifted/",

                 }
        for cate_source, url in tasks.items():
            yield feapder.Request(url, cate_source=cate_source)

    def start_requests(self, task):
        id, cate_source, url = task  # id， url为所取的字段，main函数中指定的
        yield feapder.Request(url=url, task_id=id, cate_source=cate_source)

    def parse(self, request, response):
        cate_level = request.cate_level if hasattr(
            request, "cate_level") else 0
        cate_father_cid = request.cate_father_cid if hasattr(
            request, "cate_father_cid") else None
        cate_source = request.cate_source

        cate_name = response.css(
            "span._p13n-zg-nav-tree-all_style_zg-selected__1SfhQ ::text").extract_first()
        if cate_name:
            cate_url = request.url

            father_item = AmazonCategoryItem()
            father_item.cate_name = cate_name
            father_item.cate_level = cate_level+1
            father_item.cate_url = cate_url
            father_item.cate_source = cate_source
            father_item.cate_father_cid = cate_father_cid
            father_item.cid = father_item.gen_id()
            is_children_nodes = response.xpath(
                "//span[@class='_p13n-zg-nav-tree-all_style_zg-selected__1SfhQ']/ancestor::*[1]//following-sibling::div[@role='group']")
            if not is_children_nodes:
                father_item.is_floor = 1
            yield father_item
            if is_children_nodes:
                # _category = re.findall('treeitem.*?/(Best-Sellers-?.*?)">(.*?)<', is_children_node[0])
                for i in is_children_nodes.css('div[role=treeitem]'):
                    url = i.css('a ::attr(href)').get()
                    if 'https' not in url:
                        url = self.host+url
                    name = i.css('a ::text').get()
                    logger.info(
                        f"父类:{father_item.cate_father_cid} 层级:{father_item.cate_level},名称:{name}-cid:{father_item.cid}")
                    yield feapder.Request(url=url, cate_source=cate_source, cate_level=father_item.cate_level, cate_father_cid=father_item.cid, callback=self.parse)

            if not response.css('.a-carousel-heading.a-inline-block'):  # 非一级类目
                for item in self.parse_asin(request, response, cid=father_item.cid):
                    yield item
            else:
                print("非一级类目，不入库")

    def parse_asin(self, request, response, cid):
        asins_css = response.css('div#gridItemRoot')
        for asin_css in asins_css:
            asin = asin_css.css("div::attr(data-asin)").extract_first()
            if not asin:
                continue
            print('asin', asin)
            url = asin_css.css(f"#{asin}  a::attr(href)").extract_first()
            if 'https' not in url:
                url = self.host+url
            item = AmazonCategoryAsinsItem()
            item.asin = asin
            item.cid = cid
            _rank = asin_css.css(
                "span.zg-bdg-text ::text").extract_first() or ''
            if '#' in _rank:
                _rank.replace("#", "")
            item.rank = _rank
            item.title = asin_css.css(f"#{item.asin} ::text").extract_first()
            item.url = url
            item.price = asin_css.css(
                "span[class*='-sc-price'] ::text").extract_first()
            item.rate_num = asin_css.css(
                "a[title*='out of'] span.a-size-small ::text").extract_first()
            item.score = asin_css.css(
                "a[title*='out of'] ::text").extract_first()
            print("item.score", item.score)
            item.cover = asin_css.css(
                f"#{item.asin}   img::attr(src)").extract_first()
            item.sold_rank_text = asin_css.css(
                "span[class*='rank-metadata'] ::text").extract()
            item.aid = item.gen_id()
            yield item
            self.insert_asin(asin, cid)


if __name__ == "__main__":
    AmazonCategorySpider().start()
